<?php
include '../data/category_dal.php';

class CategoryMng
{
    function update($categoryEntity)
    {
        $categoryDAL = new CategoryDAL();
        if (!($categoryDAL->validate($categoryEntity->id, $categoryEntity->category))) {
            echo "false";
            return false;
            exit;
        } else {
            $categoryDAL->update($categoryEntity->id, $categoryEntity->category,$categoryEntity->parentID,$categoryEntity->inTime,$categoryEntity->inUser);
            return true;
        }
    }

    function delete($id)
    {
        $categoryDAL = new CategoryDAL();
        return $categoryDAL->delete($id);
    }

    function validate($id, $category)
    {
        $categoryDAL = new UserDAL();
        return $categoryDAL->validate($id, $category);
    }

    function getAll($start, $pageSize, $sortField, $sortDirection)
    {
        $categoryDAL = new CategoryDAL();
        switch($pageSize) {
                case "-1":
                    $orderBy = "-1";
                    break;
                case "-2":
                    $orderBy ="-2";
                    break;
                default:
                    $orderBy = "ORDER BY " . $sortField . " " . $sortDirection . " LIMIT " . $start . ", " . ($pageSize * ($start + 1));
                    break;
            }
        if ($result = $categoryDAL->getAll($orderBy)) {
            while ($row = $result->fetch_array()) {
                $categoryList[] = $row;
            }
            return $categoryList;
        } else {
            die("Getall failed: " . $mysqli->errno);
        }
    }
    
    function get($where){
        $categoryDAL = new CategoryDAL();
        if($result = $categoryDAL->get($where)){
            return $row = $result->fetch_array();
        }else{
            die("Get data failed!");
        }
    }
}

?>
